package com.goalias.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;


import java.util.List;

@Component
public class GoaliasGatewayFilterFactory extends AbstractGatewayFilterFactory<GoaliasGatewayFilterFactory.Config> {
    public GoaliasGatewayFilterFactory() {
        super(GoaliasGatewayFilterFactory.Config.class);
    }

    @Override
    public GatewayFilter apply(GoaliasGatewayFilterFactory.Config config) {
        return (exchange, chain) -> {
            ServerHttpRequest request = exchange.getRequest();
            if (request.getQueryParams().containsKey("goalias")){
                return chain.filter(exchange);
            }else {
                exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST);
                return exchange.getResponse().setComplete();
            }
        };
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return List.of("status");
    }

    public static class Config {
        private String status;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }
    }



}
